package com.houzehui.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.houzehui.product.entity.*;
import com.houzehui.product.mapper.SkuMapper;
import com.houzehui.product.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.houzehui.product.vo.SkuDteailVo;
import com.houzehui.product.vo.SpecGroupVo;
import com.houzehui.product.vo.SpecParamVo;
import com.houzehui.product.vo.SpecParamVoo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 服务实现类
 * </p>
 *
 * @author houzehui
 * @since 2022-04-27
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements ISkuService {


    @Autowired

    ISpecGroupService iSpecGroupService;

    @Autowired

    ISpecParamService iSpecParamService;

    @Autowired

    ISpuService iSpuService;



    @Resource
    ISpuDetailService iSpuDetailService;

    @Resource
    ICategoryService iCategoryService;

    @Resource
    ICategoryBrandService iCategoryBrandService;

    @Resource
    IBrandService iBrandService;
    @Override
    public SkuDteailVo getDetailBySkuId(Long skuId) {

        //通过skuid查询商品基本信息(标题,图片,品牌,分类)
//        SkuDteailVo skuDetailVo = getBaseMapper().getDetailBySkuId(skuId);
        SkuDteailVo skuDetailVo = getSkuDetailVo(skuId);

        System.err.println(skuDetailVo);



        //根据分类id获取所有组内容
        List<SpecGroupVo> specGroupVo = iSpecGroupService.getSpecGroupVo(skuDetailVo.getCid3());

        //设置组的通用规格参数
        specGroupVo.forEach(s->{

            List<SpecParamVo> specParamVo = iSpecParamService.getSpecParamVo(skuDetailVo.getCid3(),s.getId());
            System.out.println(specParamVo);

            //查询json
            SpuDetail spuDetail = iSpuDetailService.getById(skuDetailVo.getSpuId());
            String genericSpec = spuDetail.getGenericSpec();
            String ownSpec = skuDetailVo.getOwnSpec();
            String prefix = genericSpec.substring(0, genericSpec.lastIndexOf("}"));
            String suffix = ownSpec.substring(1);


            JSONObject genericSpecjsonObject = JSON.parseObject(prefix+","+suffix);
            specParamVo.forEach(specParamVo1->{
                specParamVo1.setValue(genericSpecjsonObject.getString(specParamVo1.getId()+""));
            });


            System.out.println(specParamVo);

            s.setParamVoList(specParamVo);
        });

            skuDetailVo.setGroupVoList(specGroupVo);


        return skuDetailVo;




    }


    @Override
    public SkuDteailVo getSkuDetailVo(Long skuId) {
        SkuDteailVo skuDetailVo = new SkuDteailVo();
        Sku sku = getById(skuId);
        BeanUtil.copyProperties(sku,skuDetailVo);

        //查询商品基本信息
        Spu spu = iSpuService.getById(sku.getSpuId());
        SpuDetail spuDetail = iSpuDetailService.getById(sku.getSpuId());

        //spu包装信息 售后信息
        skuDetailVo.setAfterService(spuDetail.getAfterService());
        skuDetailVo.setPackingList(spuDetail.getPackingList());
        skuDetailVo.setDescription(spuDetail.getDescription());

        //查询分类信息
        Category category = iCategoryService.getById(spu.getCid3());
        skuDetailVo.setCid3(category.getId());
        skuDetailVo.setCategoryName(category.getName());

        //品牌信息
        Brand brand = iBrandService.getById(spu.getBrandId());
        skuDetailVo.setBrandName(brand.getName());





        return skuDetailVo;
    }




}
